package net.kucoe.elvn.control;

import java.util.List;

import net.kucoe.elvn.*;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.*;
import android.widget.*;

/**
 * Task list control
 * 
 * @author Vitaliy Basyuk
 */
public class TaskAdapter extends ArrayAdapter<Task> {
    
    private LayoutInflater inflater;
    private List<Task> tasks;
    private TypedArray buttons;
    private int viewResourceId;
    
    /**
     * Constructs TaskAdapter.
     * 
     * @param ctx
     * @param viewResourceId
     * @param tasks
     * @param buttons
     */
    public TaskAdapter(final Context ctx, final int viewResourceId, final List<Task> tasks, final TypedArray buttons) {
        super(ctx, viewResourceId, tasks);
        this.tasks = tasks;
        this.buttons = buttons;
        this.viewResourceId = viewResourceId;
        inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        Task item = getItem(position);
        View view = inflater.inflate(viewResourceId, null);
        
        Button button = (Button) view.findViewById(R.id.task_button);
        button.setBackgroundDrawable(buttons.getDrawable(getColorIndex(item)));
        
        TextView tv = (TextView) view.findViewById(R.id.task_text);
        tv.setText(item.getText());
        
        return view;
    }
    
    private int getColorIndex(final Task task) {
        ListColor color = ListColor.color(task.getList());
        switch (color) {
            case Blue:
                return 1;
            case Green:
                return 2;
            case Yellow:
                return 3;
            default:
                return 0;
        }
    }
}
